package com.my.blog.myblog.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.blog.annotation.SystemLog;
import com.my.blog.myblog.entity.Resource;
import com.my.blog.myblog.entity.custom.StatusCount;
import com.my.blog.myblog.service.ResourceService;
import com.my.blog.utils.ConstantUtil;
import com.my.blog.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/2 16:47
 * @Description： 后台资源的controller
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminResourceController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/deleteResource")
    @SystemLog(description = ConstantUtil.RESOURCE_DELETE,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deleteResource(@RequestParam(value = "id")String id){
        ResultBean<Object> resultBean=new ResultBean<>();
        if(resourceService.removeById(id)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    //修改资源
    @PostMapping("/updateResource")
    @SystemLog(description = ConstantUtil.RESOURCE_UPDATE,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updateResource(Resource resource){
        ResultBean<Object> resultBean=new ResultBean<>();
        if(resourceService.updateById(resource)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    //新增资源
    @PostMapping("/addResource")
    @SystemLog(description = ConstantUtil.RESOURCE_ADD,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> addResource(Resource resource){
        ResultBean<Object> resultBean=new ResultBean<>();
        resource.setAddTime(LocalDateTime.now());
        if(resourceService.save(resource)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    //根据id查询资源
    @PostMapping("selectResourceById")
    public ResultBean<Resource> selectResourceById(@RequestParam(value = "id")String id){
        ResultBean<Resource> resultBean=new ResultBean<>();
        Resource resource = resourceService.getById(id);
        if(ObjectUtil.isNotEmpty(resource)){
            resultBean.setData(resource);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    //查询各种状态资源数
    @GetMapping("/selectResourceListByStatus")
    public ResultBean<List<StatusCount>> selectResourceListByStatus(){
        List<Resource> resourceList = resourceService.list();
        List<StatusCount> scList=new ArrayList<>();
        int i=0;
        int j=0;
        for(Resource resource:resourceList){
            if(-1==resource.getStatus()){
                i++;
            }
            if(1==resource.getStatus()){
                j++;
            }
        }
        scList.add(new StatusCount(-1,i));
        scList.add(new StatusCount(1,j));
        return new ResultBean<>(scList);
    }

    @PostMapping("/selectLikeResourceListByPage")
    //@SystemLog(description = ConstantUtil.RESOURCE_FINDKEY,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<IPage<Resource>> selectLikeResourceListByPage(Resource resource,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        QueryWrapper<Resource> wrapper=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(resource.getStatus().toString())){
            wrapper.lambda().eq(Resource::getStatus,resource.getStatus());
        }
        if(StrUtil.isNotEmpty(resource.getContent())){
            wrapper.lambda().like(Resource::getContent,resource.getContent());
        }
        if(StrUtil.isNotEmpty(resource.getTitle())){
            wrapper.lambda().like(Resource::getTitle,resource.getTitle());
        }
        ResultBean<IPage<Resource>> resultBean=new ResultBean<>();
        Page<Resource> resourcePage = resourceService.page(new Page<>(page, pageSize), wrapper);
        if(ObjectUtil.isNotEmpty(resourcePage.getRecords())){
            resultBean.setData(resourcePage);
        }else {
            resourcePage.setTotal(0);
            resultBean.setData(resourcePage);
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
